using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Threading;

public class PlayerController : MonoBehaviour
{
    public float Health {
        set 
        {
            health = value;
            if (health <= 0)
            {
                Defeated();
            }
        }
        get
        {
            return health;
        }
    }

    public Image healthBar;
    public Button button1;
    public Button button2;

    private float health = 10f;
    public float moveSpeed = 5f;
    public float collisionOffset = 0.05f;
    public ContactFilter2D movementFilter;
    public MeleeAttack meleeAttack;
    Vector2 movementInput;
    Rigidbody2D rb;
    List<RaycastHit2D> castCollisions = new List<RaycastHit2D>();
    public Animator animator;
    SpriteRenderer spriteRenderer;
    bool canMove = true;
    private AudioSource playerStepSound;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        spriteRenderer = GetComponent<SpriteRenderer>();
        playerStepSound = GetComponent<AudioSource>();
    }


    void OnMenu(){
        Time.timeScale = 0;
        button1.gameObject.SetActive(true);
        button2.gameObject.SetActive(true);
    }

    void Defeated()
    {
        canMove = false;
        animator.Play("Player_death");
    }
    
    public void Death()
    {
        SceneManager.LoadScene("SampleScene");
    }

    void FixedUpdate()
    {
        if (canMove)
        {
            if(movementInput != Vector2.zero)
            {
                bool success = TryMove(movementInput);

                if(!success) 
                {
                    success = TryMove(new Vector2(movementInput.x, 0));
                }

                if(!success)
                {
                    success = TryMove(new Vector2(0, movementInput.y));
                }
                animator.SetBool("isMoving", success);
                
            } else 
            {
                animator.SetBool("isMoving", false);
            }
        }

        if(movementInput.x < 0) 
        {
            spriteRenderer.flipX = true;
        }
        else if (movementInput.x > 0)
        {
            spriteRenderer.flipX = false;
        }
    }
    
    private bool TryMove(Vector2 direction) 
    {
        if (direction != Vector2.zero){
            int count = rb.Cast(
                direction,
                movementFilter,
                castCollisions,
                moveSpeed * Time.fixedDeltaTime + collisionOffset);
            if (count == 0)
            {
                rb.MovePosition(rb.position + direction * moveSpeed * Time.fixedDeltaTime);
                return true;
            } else
            {
                return false;
            }
        } else {
            return false;
        }
    }

    void OnMove(InputValue movementValue)
    {
        movementInput = movementValue.Get<Vector2>();
    }

    void OnFire()
    {
        animator.SetTrigger("Attack");
    }

    public void StartAttack()
    {
        meleeAttack.StartAttack(spriteRenderer.flipX);
    }

    public void EndAttack()
    {
        meleeAttack.StopAttack();
    }

    void OnHit(float damage)
    {
        Health-=damage;
        healthBar.fillAmount = Mathf.Clamp(Health / 10f, 0, 100);
    }

    void DoWalkSound(){
        playerStepSound.Play();
    }
}
